package com.csc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.RequestDispatcher;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.csc.attributes.UserDAO;
import com.csc.attributes.UserDb;
import com.mysql.jdbc.PreparedStatement;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet("/RegisterUser")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	// User DB
	// UserDb db = null;
	// public Connection getConnection(ServletConfig config) {
	// Connection con = null;
	// try {
	// Class.forName("com.mysql.jdbc.Driver");
	// con = DriverManager.getConnection(db.getUrl(), db.getDbUsername(),
	// db.getDbPassword());
	//
	// } catch (ClassNotFoundException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// } catch (SQLException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return con;
	//
	// // Open a connection
	//
	// }

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Servlet#init(ServletConfig)
	 */
	public void init(ServletConfig config) throws ServletException {
		// db = (UserDb) config.getServletContext().getAttribute("db");
		// getConnection(config);
	}

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter outPrintWriter = response.getWriter();
		HttpSession session = request.getSession();
		String action = request.getParameter("btnSubmit");
		if (action.equalsIgnoreCase("Register")) {
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			UserDAO userDAO = new UserDAO();
			boolean checkLogin = false;
			try {
				checkLogin = userDAO.checkLogin(username, password);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (checkLogin == true) {
				session.setAttribute("errorMes", "Exist account");

			}

			RequestDispatcher rDispatcher = request
					.getRequestDispatcher("register.jsp");
			rDispatcher.forward(request, response);
		}

	}

}
